package data

import (
	"github.com/go-kratos/kratos/v2/log"
	"github.com/google/wire"
	"shop-demo/app/user/service/internal/conf"
	"shop-demo/app/user/service/internal/data/ent"
)

var ProvideSet = wire.NewSet(NewEntClient, NewData, NewUserRepo)

type Data struct {
	db *ent.Client
}

func NewEntClient(conf *conf.Data, logger log.Logger) *ent.Client {
	log := log.NewHelper(logger)
	client, err := ent.Open(conf.Database.Driver, conf.Database.Source)
	if err != nil {
		log.Fatalf("failed open connection to db : %v", err)
	}

	return client
}

func NewData(db *ent.Client) (*Data, func()) {
	return &Data{
			db: db,
		}, func() {
			err := db.Close()
			if err != nil {
				return
			}
		}
}
